package io.wdb.common.utils;

import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Workbook;

/**
 * 
 * @ClassName: LeadExportUtil.java
 * @Description:导入导出功能
 * @Time 2017年11月29日
 * @author: Tom
 */
public class LeadExportUtil {
	/**
	 * 
	 * @Method:intoServer
	 * @Description:将数据库查询到的数据写入到服务器，格式为xls
	 * @Time 2017年11月29日 上午10:53:01
	 * @author: Tom
	 * @param list
	 * @param fileName
	 * @return
	 */
	public static String intoServerXls(List<? extends Object> list,String fileName) {
		HSSFWorkbook workbook = new HSSFWorkbook();
		HSSFSheet sheet = workbook.createSheet("one");
		int rowNum = 0;
		for(Object obj:list) {
			try {
				writeObj(obj, sheet, rowNum);
			} catch (Exception e) {
				e.printStackTrace();
			}
			rowNum++;
		}
		FileOutputStream fos= null;
		try {
			fos = new FileOutputStream(fileName+DateFormatUtil.formatDate(new Date())+".xls");
			workbook.write(fos);
			fos.flush();
			return fileName+DateFormatUtil.formatDate(new Date())+".xls";
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if(fos!=null) {
					fos.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}
	private static void  writeObj(Object obj,HSSFSheet sheet,int rowNum) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
		Workbook workbook = new HSSFWorkbook();
		HSSFCellStyle cellStyle = (HSSFCellStyle) workbook.createCellStyle();    
		cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 居中    		
		
		HSSFCellStyle headStyle = (HSSFCellStyle) workbook.createCellStyle();
		HSSFFont font = (HSSFFont) workbook.createFont();
		font.setFontHeightInPoints((short)16);
		font.setColor(HSSFColor.RED.index);
		headStyle.setFont(font);
		
		HSSFCellStyle errorStyle = (HSSFCellStyle) workbook.createCellStyle();
		HSSFFont fontError = (HSSFFont) workbook.createFont();
		//fontError.setFontHeightInPoints((short)16);
		fontError.setColor(HSSFColor.YELLOW.index);
		errorStyle.setFont(fontError);
		
		HSSFRow rowHead = null;
		HSSFRow row = null;
		if(rowNum==0) {
			rowHead = sheet.createRow(rowNum);
		}
		row = sheet.createRow(rowNum+1);
		Method[] methods = obj.getClass().getDeclaredMethods();
		int i = 0;
		int errorTag = 0;
		String returnValue = null;
		for(Method method:methods) {
			String methodName = method.getName();
			if(methodName.startsWith("get")) {
				Object returnObj = method.invoke(obj, null);
				if(returnObj!=null) {
					for(int count=0;count<methods.length;count++) {
						sheet.autoSizeColumn(count, true);
					}
					if(rowNum==0) {
						
						Cell cellHead = rowHead.createCell(i);
						if(methodName.replaceAll("get", "").equals("IsbnNo")) {
							cellHead.setCellValue("Isbn");
						}
						if(methodName.replaceAll("get", "").equals("Describe")) {
							cellHead.setCellValue("描述");
						}
						if(methodName.replaceAll("get", "").equals("Brief")) {
							cellHead.setCellValue("简介");
						}
						if(methodName.replaceAll("get", "").equals("PublicationDate")) {
							cellHead.setCellValue("出版日期");
						}
						if(methodName.replaceAll("get", "").equals("ReadAge")) {
							cellHead.setCellValue("年龄段");
						}
						if(methodName.replaceAll("get", "").equals("Press")) {
							cellHead.setCellValue("出版社");
						}
						if(methodName.replaceAll("get", "").equals("Price")) {
							cellHead.setCellValue("价格");
						}
						if(methodName.replaceAll("get", "").equals("Recommendation")) {
							cellHead.setCellValue("推荐度");
						}
						if(methodName.replaceAll("get", "").equals("Author")) {
							cellHead.setCellValue("作者");
						}
						if(methodName.replaceAll("get", "").equals("BookName")) {
							cellHead.setCellValue("书名");
						}
						if(methodName.replaceAll("get", "").equals("Recommendationer")) {
							cellHead.setCellValue("推荐人");
						}
						if(methodName.replaceAll("get", "").equals("Category")) {
							cellHead.setCellValue("类别标签");
						}
						cellHead.getCellStyle().cloneStyleFrom(headStyle);
					}
					if(returnObj.getClass().equals(Date.class)) {
						returnValue = DateFormatUtil.formatDateTime((Date)returnObj);
					}else {
						returnValue = returnObj.toString();
						if(returnValue.equals("-")) {
							errorTag=1;
						}
					}
					Cell cell = row.createCell(i);
					cell.setCellValue(returnValue);
					if(rowNum>0&&errorTag==0) {
						cell.getCellStyle().cloneStyleFrom(cellStyle);
					}
					i++;
					
				}
			}
		}
	}
}
